using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using Oracle.ManagedDataAccess.Client;
using Oracle.ManagedDataAccess.Types;

namespace OracleInternal.Common
{
	public static class OracleMetadata
	{
		private static char[] double_quotes = new char[1]
		{
			'"'
		};

		private static string atSign = "@";

		public static IEnumerable<OracleLpTableColumns> GetColumnInformation(OracleConnection con, IEnumerable<OracleLpTable> tables)
		{
			List<string> list = new List<string>();
			bool flag = false;
			foreach (OracleLpTable table in tables)
			{
				if (!string.IsNullOrEmpty(table.m_dbLinkName))
				{
					if (!list.Contains(table.m_dbLinkName))
					{
						list.Add(table.m_dbLinkName);
					}
				}
				else
				{
					flag = true;
				}
			}
			if ((list.Count == 1 && !flag) || list.Count == 0)
			{
				string dbLinkName = string.Empty;
				if (list.Count == 1)
				{
					dbLinkName = list[0];
				}
				return GetColumnInfoWithSameDbLink(con, tables, dbLinkName);
			}
			return GetColumnInfoWithDiffDbLinks(con, tables);
		}

		public static IEnumerable<OracleLpTableColumns> GetColumnInfoWithSameDbLink(OracleConnection con, IEnumerable<OracleLpTable> tables, string dbLinkName)
		{
			int num = 0;
			bool flag = false;
			List<OracleLpTableColumns> list = new List<OracleLpTableColumns>();
			bool flag2 = false;
			List<string> list2 = new List<string>();
			List<string> list3 = new List<string>();
			List<string> list4 = new List<string>();
			new List<OracleRefCursor>();
			string empty = string.Empty;
			string value = string.Empty;
			_ = ((List<OracleLpTable>)tables).Count;
			if (!string.IsNullOrEmpty(dbLinkName))
			{
				flag = true;
				value = atSign + dbLinkName;
			}
			else
			{
				dbLinkName = string.Empty;
			}
			foreach (OracleLpTable table in tables)
			{
				empty = ((!string.IsNullOrEmpty(table.m_schemaName)) ? table.m_schemaName : (table.m_schemaName = ((con.m_cs.m_userId[0] != '"') ? con.m_cs.m_userId.ToUpperInvariant() : con.m_cs.m_userId.Trim(double_quotes))));
				if (flag)
				{
					string text = string.Empty;
					if (table.m_dbLinkName.Contains("@"))
					{
						string[] array = table.m_dbLinkName.Split('@');
						dbLinkName = array[0];
						text = atSign + array[1].ToUpperInvariant();
					}
					else
					{
						dbLinkName = table.m_dbLinkName;
					}
					dbLinkName = ((dbLinkName[0] != '"') ? dbLinkName.ToUpperInvariant() : dbLinkName.Trim(double_quotes).ToUpperInvariant());
					dbLinkName = (dbLinkName.Contains(".") ? (dbLinkName + text) : (dbLinkName + "." + con.DatabaseDomainName.ToUpperInvariant() + text));
				}
				OracleLpTableColumns oracleLpTableColumns = (flag ? con.m_oracleConnectionImpl.m_pm.m_tableColumnsCache.Get(con.m_oracleConnectionImpl.ServiceName, empty, table.m_tableName + "@" + dbLinkName) : con.m_oracleConnectionImpl.m_pm.m_tableColumnsCache.Get(con.m_oracleConnectionImpl.ServiceName, empty, table.m_tableName));
				if (oracleLpTableColumns == null)
				{
					if (!flag)
					{
						list3.Add(empty);
					}
					else
					{
						list4.Add(dbLinkName);
					}
					list2.Add(table.m_tableName);
					num++;
					flag2 = true;
					list.Add(null);
				}
				else
				{
					list.Add(oracleLpTableColumns);
				}
			}
			if (flag2)
			{
				StringBuilder stringBuilder = new StringBuilder(SQLMetaData.pSqlBlock1);
				stringBuilder.Append(":1");
				if (!flag)
				{
					stringBuilder.Append(SQLMetaData.pSqlBlock2);
				}
				else
				{
					stringBuilder.Append(SQLMetaData.pSqlBlock5);
				}
				stringBuilder.Append(":2");
				stringBuilder.Append(SQLMetaData.pSqlBlock3a);
				if (flag)
				{
					stringBuilder.Append(value);
				}
				stringBuilder.Append(SQLMetaData.pSqlBlock3b);
				if (flag)
				{
					stringBuilder.Append(value);
				}
				stringBuilder.Append(SQLMetaData.pSqlBlock3c);
				stringBuilder.Append(SQLMetaData.pSqlBlock14);
				stringBuilder.Append(SQLMetaData.pSqlBlock18);
				stringBuilder.Append(SQLMetaData.pSqlBlock7);
				stringBuilder.Append(":3");
				stringBuilder.Append(SQLMetaData.pSqlBlock15);
				stringBuilder.Append(SQLMetaData.pSqlBlock16);
				stringBuilder.Append(SQLMetaData.pSqlBlock7);
				stringBuilder.Append(":3");
				stringBuilder.Append(SQLMetaData.pSqlBlock6);
				if (flag)
				{
					stringBuilder.Append(value);
				}
				stringBuilder.Append(SQLMetaData.pSqlBlock8);
				stringBuilder.Append(SQLMetaData.pSqlBlock17);
				stringBuilder.Append(SQLMetaData.pSqlBlock9);
				OracleCommand oracleCommand = new OracleCommand(stringBuilder.ToString(), con);
				OracleParameter oracleParameter = new OracleParameter(":1", OracleDbType.Varchar2, ParameterDirection.Input);
				oracleParameter.Value = list2.ToArray();
				oracleCommand.Parameters.Add(oracleParameter);
				if (!flag)
				{
					OracleParameter oracleParameter2 = new OracleParameter(":2", OracleDbType.Varchar2, ParameterDirection.Input);
					oracleParameter2.Value = list3.ToArray();
					oracleCommand.Parameters.Add(oracleParameter2);
				}
				else
				{
					OracleParameter oracleParameter3 = new OracleParameter(":2", OracleDbType.Varchar2, ParameterDirection.Input);
					oracleParameter3.Value = list4.ToArray();
					oracleCommand.Parameters.Add(oracleParameter3);
				}
				OracleParameter param = new OracleParameter(":3", OracleDbType.RefCursor, ParameterDirection.Output);
				oracleCommand.Parameters.Add(param);
				oracleCommand.ArrayBindCount = num;
				OracleDataReader oracleDataReader = null;
				try
				{
					oracleDataReader = oracleCommand.ExecuteReader();
					int num2 = 0;
					foreach (OracleLpTable table2 in tables)
					{
						num2++;
						if (list[num2 - 1] != null)
						{
							continue;
						}
						List<OracleLpColumn> list5 = new List<OracleLpColumn>();
						while (oracleDataReader.Read())
						{
							string @string = oracleDataReader.GetString(0);
							OracleLpColumn oracleLpColumn = new OracleLpColumn(@string);
							oracleLpColumn.m_schemaName = oracleDataReader.GetString(1);
							oracleLpColumn.m_tableName = oracleDataReader.GetString(2);
							string string2 = oracleDataReader.GetString(3);
							if (string2 != null)
							{
								oracleLpColumn.m_isHidden = string2.Equals("yes", StringComparison.InvariantCultureIgnoreCase);
							}
							if (ProviderConfig.m_bTraceLevelPrivate)
							{
								Trace.Write(OracleTraceLevel.Private, OracleTraceTag.Error, OracleTraceClassName.OracleMetadata, OracleTraceFuncName.GetColumnInfoWithSameDbLink, "Retrieved Column from data dictionary for {0}.{1} : {2} , Hidden: {3}", oracleLpColumn.m_schemaName, oracleLpColumn.m_tableName, @string, oracleLpColumn.m_isHidden.ToString());
							}
							list5.Add(oracleLpColumn);
						}
						OracleLpTableColumns tableColumns = (list[num2 - 1] = new OracleLpTableColumns(table2, list5));
						if (flag)
						{
							string text2 = string.Empty;
							if (table2.m_dbLinkName.Contains("@"))
							{
								string[] array2 = table2.m_dbLinkName.Split('@');
								dbLinkName = array2[0];
								text2 = atSign + array2[1].ToUpperInvariant();
							}
							else
							{
								dbLinkName = table2.m_dbLinkName;
							}
							dbLinkName = ((dbLinkName[0] != '"') ? dbLinkName.ToUpperInvariant() : dbLinkName.Trim(double_quotes).ToUpperInvariant());
							dbLinkName = (dbLinkName.Contains(".") ? (dbLinkName + text2) : (dbLinkName + "." + con.DatabaseDomainName.ToUpperInvariant() + text2));
						}
						if (flag)
						{
							con.m_oracleConnectionImpl.m_pm.m_tableColumnsCache.Put(con.m_oracleConnectionImpl.ServiceName, table2.m_schemaName, table2.m_tableName + "@" + dbLinkName, tableColumns);
						}
						else
						{
							con.m_oracleConnectionImpl.m_pm.m_tableColumnsCache.Put(con.m_oracleConnectionImpl.ServiceName, table2.m_schemaName, table2.m_tableName, tableColumns);
						}
						oracleDataReader.NextResult();
					}
					return list;
				}
				finally
				{
					oracleDataReader?.Close();
				}
			}
			return list;
		}

		public static IEnumerable<OracleLpTableColumns> GetColumnInfoWithDiffDbLinks(OracleConnection con, IEnumerable<OracleLpTable> tables)
		{
			int num = 0;
			int num2 = 1;
			string text = string.Empty;
			string empty = string.Empty;
			bool flag = false;
			string value = string.Empty;
			List<OracleLpTableColumns> list = new List<OracleLpTableColumns>();
			StringBuilder stringBuilder = new StringBuilder(SQLMetaData.pSqlBlock1);
			OracleCommand oracleCommand = new OracleCommand();
			bool flag2 = false;
			foreach (OracleLpTable table in tables)
			{
				if (string.IsNullOrEmpty(table.m_dbLinkName))
				{
					text = ((!string.IsNullOrEmpty(table.m_schemaName)) ? table.m_schemaName : (table.m_schemaName = ((con.m_cs.m_userId[0] != '"') ? con.m_cs.m_userId.ToUpperInvariant() : con.m_cs.m_userId.Trim(double_quotes))));
					flag = false;
				}
				else
				{
					string text2 = string.Empty;
					value = atSign + table.m_dbLinkName;
					if (table.m_dbLinkName.Contains("@"))
					{
						string[] array = table.m_dbLinkName.Split('@');
						empty = array[0];
						text2 = atSign + array[1].Trim(double_quotes).ToUpperInvariant();
					}
					else
					{
						empty = table.m_dbLinkName;
					}
					empty = ((empty[0] != '"') ? empty.ToUpperInvariant() : empty.Trim(double_quotes).ToUpperInvariant());
					text = (empty.Contains(".") ? (empty + text2) : (empty + "." + con.DatabaseDomainName.ToUpperInvariant() + text2));
					flag = true;
				}
				OracleLpTableColumns oracleLpTableColumns = (flag ? con.m_oracleConnectionImpl.m_pm.m_tableColumnsCache.Get(con.m_oracleConnectionImpl.ServiceName, table.m_schemaName, table.m_tableName + "@" + text) : con.m_oracleConnectionImpl.m_pm.m_tableColumnsCache.Get(con.m_oracleConnectionImpl.ServiceName, table.m_schemaName, table.m_tableName));
				if (oracleLpTableColumns == null)
				{
					string text3;
					string text4;
					if (num == 0)
					{
						text3 = string.Format(SQLMetaData.pSqlBlock11, num2++);
						stringBuilder.Append(text3);
						if (!flag)
						{
							stringBuilder.Append(SQLMetaData.pSqlBlock2);
							text4 = string.Format(SQLMetaData.pSqlBlock11, num2++);
							stringBuilder.Append(text4);
						}
						else
						{
							text4 = string.Format(SQLMetaData.pSqlBlock11, num2++);
							stringBuilder.Append(SQLMetaData.pSqlBlock5);
							stringBuilder.Append(text4);
						}
					}
					else
					{
						stringBuilder.Append(SQLMetaData.pSqlBlock10);
						text3 = string.Format(SQLMetaData.pSqlBlock11, num2++);
						stringBuilder.Append(SQLMetaData.pSqlBlock12);
						stringBuilder.Append(text3);
						if (!flag)
						{
							text4 = string.Format(SQLMetaData.pSqlBlock11, num2++);
							stringBuilder.Append(SQLMetaData.pSqlBlock13);
							stringBuilder.Append(text4);
						}
						else
						{
							text4 = string.Format(SQLMetaData.pSqlBlock11, num2++);
							stringBuilder.Append(SQLMetaData.pSqlBlock5);
							stringBuilder.Append(text4);
						}
					}
					stringBuilder.Append(SQLMetaData.pSqlBlock3a);
					if (flag)
					{
						stringBuilder.Append(value);
					}
					stringBuilder.Append(SQLMetaData.pSqlBlock3b);
					if (flag)
					{
						stringBuilder.Append(value);
					}
					stringBuilder.Append(SQLMetaData.pSqlBlock3c);
					string text5 = string.Format(SQLMetaData.pSqlBlock11, num2++);
					stringBuilder.Append(SQLMetaData.pSqlBlock14);
					stringBuilder.Append(SQLMetaData.pSqlBlock18);
					stringBuilder.Append(SQLMetaData.pSqlBlock7);
					stringBuilder.Append(text5);
					stringBuilder.Append(SQLMetaData.pSqlBlock15);
					stringBuilder.Append(SQLMetaData.pSqlBlock16);
					stringBuilder.Append(SQLMetaData.pSqlBlock7);
					stringBuilder.Append(text5);
					stringBuilder.Append(SQLMetaData.pSqlBlock6);
					if (flag)
					{
						stringBuilder.Append(value);
					}
					stringBuilder.Append(SQLMetaData.pSqlBlock8);
					stringBuilder.Append(SQLMetaData.pSqlBlock17);
					OracleParameter oracleParameter = new OracleParameter(text3, OracleDbType.Varchar2, ParameterDirection.Input);
					oracleParameter.Value = table.m_tableName;
					oracleCommand.Parameters.Add(oracleParameter);
					OracleParameter oracleParameter2 = new OracleParameter(text4, OracleDbType.Varchar2, ParameterDirection.Input);
					oracleParameter2.Value = text;
					oracleCommand.Parameters.Add(oracleParameter2);
					OracleParameter param = new OracleParameter(text5, OracleDbType.RefCursor, ParameterDirection.Output);
					oracleCommand.Parameters.Add(param);
					flag2 = true;
					list.Add(null);
				}
				else
				{
					list.Add(oracleLpTableColumns);
				}
				num++;
			}
			if (flag2)
			{
				stringBuilder.Append(SQLMetaData.pSqlBlock9);
				oracleCommand.Connection = con;
				oracleCommand.CommandText = stringBuilder.ToString();
				OracleDataReader oracleDataReader = null;
				try
				{
					oracleDataReader = oracleCommand.ExecuteReader();
					num = 0;
					foreach (OracleLpTable table2 in tables)
					{
						num++;
						if (list[num - 1] != null)
						{
							continue;
						}
						List<OracleLpColumn> list2 = new List<OracleLpColumn>();
						while (oracleDataReader.Read())
						{
							string @string = oracleDataReader.GetString(0);
							OracleLpColumn oracleLpColumn = new OracleLpColumn(@string);
							oracleLpColumn.m_schemaName = oracleDataReader.GetString(1);
							oracleLpColumn.m_tableName = oracleDataReader.GetString(2);
							string string2 = oracleDataReader.GetString(3);
							if (string2 != null)
							{
								oracleLpColumn.m_isHidden = string2.Equals("yes", StringComparison.InvariantCultureIgnoreCase);
							}
							if (ProviderConfig.m_bTraceLevelPrivate)
							{
								Trace.Write(OracleTraceLevel.Private, OracleTraceTag.Error, OracleTraceClassName.OracleMetadata, OracleTraceFuncName.GetColumnInfoWithSameDbLink, "Retrieved Column from data dictionary for {0}.{1} : {2} , Hidden: {3}", oracleLpColumn.m_schemaName, oracleLpColumn.m_tableName, @string, oracleLpColumn.m_isHidden.ToString());
							}
							list2.Add(oracleLpColumn);
						}
						OracleLpTableColumns tableColumns = (list[num - 1] = new OracleLpTableColumns(table2, list2));
						if (string.IsNullOrEmpty(table2.m_dbLinkName))
						{
							flag = false;
						}
						else
						{
							string text6 = string.Empty;
							value = atSign + table2.m_dbLinkName;
							if (table2.m_dbLinkName.Contains("@"))
							{
								string[] array2 = table2.m_dbLinkName.Split('@');
								empty = array2[0];
								text6 = atSign + array2[1].Trim(double_quotes).ToUpperInvariant();
							}
							else
							{
								empty = table2.m_dbLinkName;
							}
							empty = ((empty[0] != '"') ? empty.ToUpperInvariant() : empty.Trim(double_quotes).ToUpperInvariant());
							text = (empty.Contains(".") ? (empty + text6) : (empty + "." + con.DatabaseDomainName.ToUpperInvariant() + text6));
							flag = true;
						}
						if (flag)
						{
							con.m_oracleConnectionImpl.m_pm.m_tableColumnsCache.Put(con.m_oracleConnectionImpl.ServiceName, table2.m_schemaName, table2.m_tableName + "@" + text, tableColumns);
						}
						else
						{
							con.m_oracleConnectionImpl.m_pm.m_tableColumnsCache.Put(con.m_oracleConnectionImpl.ServiceName, table2.m_schemaName, table2.m_tableName, tableColumns);
						}
						oracleDataReader.NextResult();
					}
					return list;
				}
				finally
				{
					oracleDataReader?.Close();
				}
			}
			return list;
		}
	}
}
